<?php 
/**
 *  Por Federico Pisarello - SisDev Software (c) 2012 - Buenos Aires, Argentina.
 * 	fpisarello@sisdevsoft.com
 */

require("includes/funciones.inc.php");

session_start();
if(!isset($_SESSION['idUser']))
	exit();

function searchABMMeasurement()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	require("includes/datatables_db/Datatables.php");
	
	$datatables = new Datatables();
	
	$datatables
			->select('mi.idMeasurementItem, mi.initTime, mi.finishTime, h.name as nameHorse, t.description, u.userCompleteName')
			->from('measurement_item mi')
			->join('measurement m','m.idMeasurement = mi.idMeasurement')
			->join('horses h','h.idHorse = mi.idHorse')
			->join('track t','t.idTrack = m.idTrack')
			->join('users u','h.idUser = u.idUser')
			->add_column('edit', '<img src="images/details_open.png">')
			->where("state <> '".$stateDeleted."'");
			
	if($_SESSION['idTypeUser'] != $idTypeAdministrator)
		$datatables->where("mi.idUser = ".$_SESSION['idUser']);

	return $datatables->generate();	
}

function viewMeasurement()
{
	require("includes/constants.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
		
	$strSQL = "SELECT mi.*, h.name, w.description As descWork FROM measurement_item mi 
					INNER JOIN horses h ON mi.idHorse = h.idHorse
					INNER JOIN work w ON mi.idWork = w.idWork
				WHERE mi.idMeasurementItem = " . $_REQUEST["idMeasurementItem"];

	$rsMeasurement = $DB->Execute($strSQL);

	if(!$rsMeasurement->EOF){
		$joMeasurement->data['horseName'] = $rsMeasurement->fields("name");
		$joMeasurement->data['initTime'] = date("d/m/Y H:m:s", strtotime($rsMeasurement->fields("initTime")));
		$joMeasurement->data['finishTime'] = date("d/m/Y H:m:s", strtotime($rsMeasurement->fields("finishTime")));
		$joMeasurement->data['lacticAcid1'] = $rsMeasurement->fields("lacticAcid1");
		$joMeasurement->data['heartRate1'] = $rsMeasurement->fields("heartRate1");
		$joMeasurement->data['lacticAcid2'] = $rsMeasurement->fields("lacticAcid2");
		$joMeasurement->data['heartRate2'] = $rsMeasurement->fields("heartRate2");
		$joMeasurement->data['lacticAcid3'] = $rsMeasurement->fields("lacticAcid3");
		$joMeasurement->data['heartRate3'] = $rsMeasurement->fields("heartRate3");
		$joMeasurement->data['respiratoryRate3'] = $rsMeasurement->fields("respiratoryRate3");
		$joMeasurement->data['work'] = $rsMeasurement->fields("descWork");
		$joMeasurement->data['meters'] = $rsMeasurement->fields("meters");
		$joMeasurement->data['time'] = $rsMeasurement->fields("time");				

		return json_encode($joMeasurement->data);
	}
	else
		return json_encode('');
}

switch($_REQUEST['actionOfForm'])
{
	case "VIEW":
		echo viewMeasurement();
		break;
	case "search":
		echo searchABMMeasurement();
		break;
}
?>
